package com.controller;

import com.common.paging.PagingData;
import com.dto.excution.LoginExcution;
import com.dto.in.AddStudentDto;
import com.dto.in.AddTeacherDto;
import com.dto.in.StudentDto;
import com.entity.CertificationInfo;
import com.entity.ChangePhoneInfo;
import com.entity.StudentInfo;
import com.entity.Teacher;
import com.enums.LoginEnum;
import com.enums.StudentEnum;
import com.service.StudentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/10/16 0016.
 */
@RestController
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @ApiOperation(value = "查学生")
    @GetMapping(value = "/findStudentList")
    @ResponseBody
    public LoginExcution<List<StudentInfo>> findStudentList(StudentDto studentDto) {
        List<StudentInfo> studentInfo = studentService.findStudentList(studentDto);
        return new LoginExcution<List<StudentInfo>>(studentInfo, StudentEnum.SUCCESS);
    }

    @ApiOperation(value = "通过ID查学生")
    @GetMapping(value = "/findStudentWhereId")
    @ResponseBody
    public LoginExcution<StudentInfo> findStudentWhereId(String userId, String studentId) {
        StudentInfo studentInfo = studentService.findStudentWhereId(Integer.parseInt(userId), Integer.parseInt(studentId));
        return new LoginExcution<StudentInfo>(studentInfo, StudentEnum.SUCCESS);
    }

    @ApiOperation(value = "通过ID查用户所有发布学生")
    @GetMapping(value = "/findStudentByUserId")
    @ResponseBody
    public LoginExcution<List<StudentInfo>> findStudentByUserId(Integer userId) {
        List<StudentInfo> studentInfo = studentService.findStudentByUserId(userId);
        return new LoginExcution<List<StudentInfo>>(studentInfo, StudentEnum.SUCCESS);
    }


    @ApiOperation(value = "查看是否认证")
    @PostMapping(value = "/findCertification")
    @ResponseBody
    public LoginExcution<Void> findCertification(Integer userId) {
        int success = studentService.findCertification(userId);
        if (success > 0) {
            return new LoginExcution<Void>(null, StudentEnum.CSUCCESS);
        } else {
            return new LoginExcution<Void>(null, StudentEnum.CFAIL);
        }
    }

    @ApiOperation(value = "发布学生信息")
    @PostMapping(value = "/addStudent")
    @ResponseBody
    public LoginExcution<Void> addStudent(AddStudentDto addStudentDto) {
        int success = studentService.addStudent(addStudentDto);
        if (success > 0) {
            return new LoginExcution<Void>(null, StudentEnum.SUCCESS);
        } else {
            return new LoginExcution<Void>(null, StudentEnum.FAIL);
        }
    }

    @ApiOperation(value = "查我的发布")
    @PostMapping(value = "/myRelease")
    @ResponseBody
    public LoginExcution<List<StudentInfo>> myRelease(Integer userId, Integer limit) {
        List<StudentInfo> studentInfo = studentService.myRelease(userId, limit);
        return new LoginExcution<List<StudentInfo>>(studentInfo, StudentEnum.SUCCESS);
    }


    @ApiOperation(value = "删除我的发布")
    @PostMapping(value = "/setMyRelease")
    @ResponseBody
    public LoginExcution<Void> setMyRelease(Integer userId, Integer studentId) {
        int success = studentService.setMyRelease(userId, studentId);
        if (success > 0) {
            return new LoginExcution<Void>(null, StudentEnum.SUCCESS);
        } else {
            return new LoginExcution<Void>(null, StudentEnum.FAIL);
        }

    }

    @ApiOperation(value = "修改家长信息")
    @PostMapping(value = "/editStudent")
    @ResponseBody
    public LoginExcution<Void> editStudentTeacher(AddStudentDto addStudentDto) {
        int success = studentService.updateStudent(addStudentDto);
        if (success > 0) {
            return new LoginExcution<Void>(null, StudentEnum.SUCCESS);
        } else {
            return new LoginExcution<Void>(null, StudentEnum.FAIL);
        }
    }


    @ApiOperation(value = "修改教师信息")
    @PostMapping(value = "/editTeacher")
    @ResponseBody
    public LoginExcution<Void> editTeacher(AddTeacherDto addTeacherDto) {
        System.out.println(addTeacherDto);
        int success = studentService.updateTeacher(addTeacherDto);
        if (success > 0) {
            return new LoginExcution<Void>(null, StudentEnum.SUCCESS);
        } else {
            return new LoginExcution<Void>(null, StudentEnum.FAIL);
        }
    }


    @ApiOperation(value = "查发布/申请/预约限制")
    @PostMapping(value = "/findLimit")
    @ResponseBody
    public LoginExcution<Void> findLimit(Integer userId, Integer type) {
        int success = studentService.findLimit(userId, type);
        if (success > 0) {
            return new LoginExcution<Void>(null, StudentEnum.SUCCESS);
        } else {
            return new LoginExcution<Void>(null, StudentEnum.FAIL);
        }
    }

    @ApiOperation(value = "查交换电话List")
    @PostMapping(value = "/findChangePhone")
    @ResponseBody
    public LoginExcution<List<ChangePhoneInfo>> findChangePhone(Integer userId, Integer type, Integer limit) {
        List<ChangePhoneInfo> list = studentService.findChangePhone(userId, type, limit);
        return new LoginExcution<List<ChangePhoneInfo>>(list, StudentEnum.SUCCESS);

    }

    @ApiOperation(value = "查是否能看这个老师的手机号")
    @PostMapping(value = "/findIsPhoneTeacher")
    @ResponseBody
    public LoginExcution<Void> findIsPhoneTeacher(Integer stdUserId, Integer teaUserId) {
        int success = studentService.findIsPhoneTeacher(stdUserId, teaUserId);
        if (success > 0) {
            return new LoginExcution<Void>(null, StudentEnum.SUCCESS);
        } else {
            return new LoginExcution<Void>(null, StudentEnum.FAIL);
        }
    }

    @ApiOperation(value = "家长请求老师交换电话")
    @PostMapping(value = "/addParentAndTeacher")
    @ResponseBody
    public LoginExcution<Void> addParentAndTeacher(Integer userId, Integer stdId, Integer teaUserId, Integer teaId) {
        int success = studentService.addParentAndTeacher(userId, stdId, teaUserId, teaId);
        if (success > 0) {
            return new LoginExcution<Void>(null, StudentEnum.SUCCESS);
        } else {
            return new LoginExcution<Void>(null, StudentEnum.FAIL);
        }
    }

    @ApiOperation(value = "家长请求老师交换电话2")
    @PostMapping(value = "/addParentAndTeacher2")
    @ResponseBody
    public LoginExcution<Void> addParentAndTeacher2(Integer userId, Integer stdId, Integer teaUserId, Integer teaId) {
        int success = studentService.addParentAndTeacher2(userId, stdId, teaUserId, teaId);
        if (success > 0) {
            return new LoginExcution<Void>(null, StudentEnum.SUCCESS);
        } else {
            return new LoginExcution<Void>(null, StudentEnum.FAIL);
        }
    }

    @ApiOperation(value = "查是否交换电话有没有数据")
    @PostMapping(value = "/findIsHaveData")
    @ResponseBody
    public LoginExcution<Void> findIsHaveData(Integer stdUserId, Integer teaUserId) {
        int success = studentService.findIsHaveData(stdUserId, teaUserId);
        if (success > 0) {
            return new LoginExcution<Void>(null, StudentEnum.SUCCESS);
        } else {
            return new LoginExcution<Void>(null, StudentEnum.FAIL);
        }
    }

    @ApiOperation(value = "同意交换电话")
    @PostMapping(value = "/agreeChangePhone")
    @ResponseBody
    public LoginExcution<Void> agreeChangePhone(Integer applyPhoneId) {
        int success = studentService.agreeChangePhone(applyPhoneId);
        if (success > 0) {
            return new LoginExcution<Void>(null, StudentEnum.SUCCESS);
        } else {
            return new LoginExcution<Void>(null, StudentEnum.FAIL);
        }
    }

    @ApiOperation(value = "删除此交换电话数据")
    @PostMapping(value = "/deleteChangePhone")
    @ResponseBody
    public LoginExcution<Void> deleteChangePhone(Integer applyPhoneId) {
        int success = studentService.deleteChangePhone(applyPhoneId);
        if (success > 0) {
            return new LoginExcution<Void>(null, StudentEnum.SUCCESS);
        } else {
            return new LoginExcution<Void>(null, StudentEnum.FAIL);
        }
    }

    @ApiOperation(value = "后台/查所有认证信息")
    @PostMapping(value = "/findCertificationList")
    @ResponseBody
    public PagingData findCertificationList(Integer page, Integer length, String search, Integer type) {
        page = (page - 1) * 10;
        PagingData list = studentService.findCertificationList(page, length, search, type);
        return list;
    }

    @ApiOperation(value = "修改认证状态")
    @PostMapping(value = "/setCertification")
    @ResponseBody
    public LoginExcution<Void> setCertification(Integer id, Integer type) {
        int update = studentService.setCertification(id, type);
        if (update > 0) {
            return new LoginExcution<Void>(null, LoginEnum.SETSUCCESS);
        } else {
            return new LoginExcution<Void>(null, LoginEnum.SETFAIL);
        }
    }

    @ApiOperation(value = "查询用户类别")
    @PostMapping(value = "/findUserType")
    @ResponseBody
    public Map findUserType() {
        Map map = studentService.findUserType();
        return map;
    }

    @ApiOperation(value = "申请预约统计")
    @PostMapping(value = "/findStatistics")
    @ResponseBody
    public Map findStatistics() {
        Map map = studentService.findStatistics();
        return map;
    }

    @ApiOperation(value = "查询UserType的List")
    @PostMapping(value = "/findUserTypeList")
    @ResponseBody
    public PagingData findUserTypeList(Integer page, Integer length, String search) {
        PagingData pagingData = studentService.findUserTypeList(page, length, search);
        return pagingData;
    }

    @ApiOperation(value = "后台/查我的发布")
    @PostMapping(value = "/houduanMyRelease")
    @ResponseBody
    public LoginExcution<List<StudentInfo>> houduanMyRelease(Integer userId, Integer limit) {
        List<StudentInfo> studentInfo = studentService.houduanMyRelease(userId, limit);
        return new LoginExcution<List<StudentInfo>>(studentInfo, StudentEnum.SUCCESS);
    }

    @ApiOperation(value = "查询所有老师的所有信息")
    @PostMapping(value = "/findAll")
    @ResponseBody
    public Map findAll(Integer page, Integer length, String search) {
        if (search.equals("显示")) {
            search = "YES";
        } else if (search.equals("隐藏")) {
            search = "NO";
        }
        Map map = new HashMap();
        List<Teacher> pagingData = studentService.findAll(page, length, search);
        int count = studentService.findCount(search);
        map.put("count", count);
        map.put("data", pagingData);
        return map;
    }
}
